package com.zjitc.controller;

import com.zjitc.po.User;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * Created by asus on 2018/9/14.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(@RequestParam(value = "username") String username,
                        @RequestParam(value = "password") String password,
                        HttpSession session,
                        Model model){
        User user = userService.findUserByUsername(username);
        if(user == null){
            model.addAttribute("usernamemsg","用户不存在!");
            return "loginandregistration/demo.jsp";
        }else{
            if(user.getPassword().equals(password)){
                session.setAttribute("user",user);
                return "personalpage/index.jsp";
            }else{
                model.addAttribute("passwordmsg","密码错误!");
                return "loginandregistration/demo.jsp";
            }
        }

    }
}
